/*
 * @Author: 赵东选 1807248216@qq.com
 * @Date: 2024-10-20 10:29:09
 * @LastEditors: 赵东选 1807248216@qq.com
 * @LastEditTime: 2024-10-23 20:54:58
 * @FilePath: /9_int/bsp/key/bsp_key.c
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
#include "bsp_key.h"
#include "../delay/bsp_delay.h"
#include "../gpio/bsp_gpio.h"

/// @brief
/// @param
void key_init(void)
{
    IOMUXC_SetPinMux(IOMUXC_UART1_CTS_B_GPIO1_IO18, 1);
    IOMUXC_SetPinConfig(IOMUXC_UART1_CTS_B_GPIO1_IO18, 0xF080);

    // GPIO1->GDIR &= ~(1 << 18);

    gpio_pin_config_t config;
    config.direction = kGPIO_DigitalInput;
    gpio_init(GPIO1, 18, &config);
}

/// @brief
/// @param
/// @return
int read_key(void)
{

    return gpio_pinread(GPIO1, 18);
}

int key_getvalue(void)
{
    int ret = 0;
    static unsigned char release = 1; // 按键释放为1

    if ((release == 1) && (read_key() == 0))
    {
        delay(10);
        release = 0;
        if (read_key() == 0)
        {
            ret = KEY0_VALUE;
        }
    }
    else if (read_key() == 1)
    {
        ret = KEY_NONE;
        release = 1;
    }
    return ret;
}
